package com.goldgov.learnmanage.service.impl;

import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.goldgov.commylog.dao.CommylogDao;
import com.goldgov.commylog.service.Commylog;
import com.goldgov.commylog.service.CommylogQuery;
import com.goldgov.learnmanage.dao.UserDao;
import com.goldgov.learnmanage.service.User;
import com.goldgov.learnmanage.service.UserParam;
import com.goldgov.learnmanage.service.UserQuery;
import com.goldgov.learnmanage.service.UserService;
import com.goldgov.organization.service.Organization;
import com.goldgov.utils.DateUtil;
import com.goldgov.utils.PropertyUtil;
@Service
public class UserServiceImpl implements UserService {

	@Autowired
	UserDao userDao;
	@Autowired
	private CommylogDao commyLogDao;
	@Override
	public List<User> getUserList(UserQuery query) {
		return userDao.getUserList(query);
	}
	@Override	
	public User findMyCourseLearing(UserQuery query) {
		User user=userDao.findMyCourseLearing(query);
		//四舍五入取整
		if(user!=null&&user.getCurrentCourseTime()>0) {
			user.setCurrentCourseTime(Integer.valueOf((int)Math.round((double)user.getCurrentCourseTime()/60)));	
		}
		//查询日志数
		CommylogQuery commyLogQuery=new CommylogQuery();
		commyLogQuery.setSearchUserID(query.getSearchUserID());
		List<Commylog> list=commyLogDao.listCommylog(commyLogQuery);
		if(list!=null) {
			if(user==null) {
				User u=new User();
				u.setCommLogNum(list.size());
				return u;
			}else {
				user.setCommLogNum(list.size());	
			}
		}
		return user;
	}
	@Override
	public List<User> findOrgCourseLearing(UserQuery query) {
		 List<User> userList= userDao.findOrgCourseLearingPageSize(query);
		 for(User user :userList) {
			 if(user!=null&&user.getCurrentCourseTime()>0) {
					user.setCurrentCourseTime(Integer.valueOf((int)Math.round((double)user.getCurrentCourseTime()/60)));	
				}
		 }
		 return userList;
	}
	@Override
	public List<User> findOrgUser(String orgID) {
		return userDao.findOrgUser(orgID);
	}
	@Override
	public List<User> getUserLearnList(UserQuery query) {
		String searchYear=query.getSearchYear();
		String searchTreePath=query.getSearchTreePath();
		boolean  searchChilderOrg=query.isSearchChilderOrg();
		String searchOrgCode=null;
		String searchUserName=null;
		String searchOrgName=null;
		String searchLoginID=null;
		Integer searchMember=null;
		if(PropertyUtil.objectNotEmpty(searchOrgCode)) {
			searchOrgCode=query.getSearchOrgCode();
		}
		if(PropertyUtil.objectNotEmpty(searchUserName)) {
			searchUserName=query.getSearchUserName();	
				}
		if(PropertyUtil.objectNotEmpty(searchLoginID)) {
			searchLoginID=query.getSearchLoginID();
		}
		if(PropertyUtil.objectNotEmpty(searchMember)) {
			searchMember=query.getSearchMember();
		}
		if(PropertyUtil.objectNotEmpty(searchOrgCode)) {
			searchOrgCode=query.getSearchOrgCode();
		}
		if(PropertyUtil.objectNotEmpty(searchOrgName)) {
			searchOrgName=searchOrgName;
		}
		query.calculate(userDao.countUserLearnListNoQuery(searchYear, searchTreePath, searchChilderOrg, searchOrgCode, searchUserName,searchOrgName, searchLoginID, searchMember));
		Integer firstResult=query.getFirstResult();
		Integer pageSize=query.getPageSize();
		return userDao.getUserLearnListNoQuery(searchYear, searchTreePath, searchChilderOrg, searchOrgCode, searchUserName,searchOrgName, searchLoginID, searchMember, firstResult, pageSize);
		//return userDao.getUserLearnList(query);
	}
	@Override
	public List<User> listUserByGender(String orgID) {
		return userDao.listUserByGender(orgID);
	}
	@Override
	public List<Organization> listOrgByType(String orgID) {
		return userDao.listOrgByType(orgID);
	}
	
	/**
	 * 获取下一级组织学习信息
	 * @date 2018年11月28日 上午11:07:41 
	 * @author hanxiangyu
	 * @param query
	 * @return
	 */
	public List<User> findOrgLearnCourseTime(UserParam query){
		if (PropertyUtil.objectNotEmpty(query.getSearchType())) {
			switch (query.getSearchType()) {
			case 1:
				query.setSearchTime(DateUtil.formatDate(new Date()));
				break;
			case 3:
				query.setSearchParam(DateUtil.getMonth(new Date()));
				break;
			case 4:
				query.setSearchParam(DateUtil.getSeason(new Date()));
				break;
			case 5:
				query.setSearchParam(DateUtil.getYear(new Date()));
				break;
			default:
				query.setSearchType(3);
				query.setSearchParam(DateUtil.getMonth(new Date()));
				break;
			}
		}else {
			query.setSearchParam(DateUtil.getMonth(new Date()));
		}
		List<User> list = userDao.findOrgLearnCourseTime(query);
		return list;
	}
	/**
	 * 获取最下级人员学习信息
	 * @date 2018年11月28日 上午11:09:29 
	 * @author hanxiangyu
	 * @param query
	 * @return
	 */
	public List<User> findOrgUserLearn(UserParam query){
		if (PropertyUtil.objectNotEmpty(query.getSearchType())) {
			switch (query.getSearchType()) {
			case 1:
				query.setSearchTime(DateUtil.formatDate(new Date()));
				break;
			case 3:
				query.setSearchParam(DateUtil.getMonth(new Date()));
				break;
			case 4:
				query.setSearchParam(DateUtil.getSeason(new Date()));
				break;
			case 5:
				query.setSearchParam(DateUtil.getYear(new Date()));
				break;
			default:
				query.setSearchType(3);
				query.setSearchParam(DateUtil.getMonth(new Date()));
				break;
			}
		}else {
			query.setSearchParam(DateUtil.getMonth(new Date()));
		}
		List<User> list = userDao.findOrgUserLearn(query);
		for (User user : list) {
			user.setAverageTime(user.getTotalTime());
		}
		return list;
	}
	/**
	 * 获取下一级组织人员对比
	 * @date 2018年11月28日 上午11:08:15 
	 * @author hanxiangyu
	 * @param query
	 * @return
	 */
	public List<User> findOrgUserContrast(UserQuery query){
		List<User> list = userDao.findOrgUserContrast(query);
		return list;
	}
	/**
	 * 获取下一级组织人员对比
	 * @date 2018年11月28日 上午11:08:15 
	 * @author hanxiangyu
	 * @param query
	 * @return
	 */
	public List<User> findOrgUserContrastByOrgID(UserParam query){
		List<User> list = userDao.findOrgUserContrastByOrgID(query);
		return list;
	}
	/**
	 * 判断是否含有下级组织
	 * @date 2018年11月28日 上午11:08:15 
	 * @author hanxiangyu
	 * @param query
	 * @return
	 */
	public List<User> judgeIsIncludeSubordinate(String orgID){
		return userDao.judgeIsIncludeSubordinate(orgID);
	}
	/**
	 * 流动党员学习
	 * @date 2018年12月13日 上午11:15:05 
	 * @author hanxiangyu
	 * @param query
	 * @return
	 */
	public List<User> findOrgLDDYStudy(UserQuery query){
		if (PropertyUtil.objectNotEmpty(query.getSearchType())) {
			switch (query.getSearchType()) {
			case 1:
				query.setSearchTime(DateUtil.formatDate(new Date()));
				break;
			case 3:
				query.setSearchParam(DateUtil.getMonth(new Date()));
				break;
			case 4:
				query.setSearchParam(DateUtil.getSeason(new Date()));
				break;
			case 5:
				query.setSearchParam(DateUtil.getYear(new Date()));
				break;
			default:
				query.setSearchType(3);
				query.setSearchParam(DateUtil.getMonth(new Date()));
				break;
			}
		}else {
			query.setSearchParam(DateUtil.getMonth(new Date()));
		}
		return userDao.findOrgLDDYStudy(query);
	}
}
